//
//  EPViewController.swift
//  EPHome
//
//  Created by liujianlong on 2022/8/1.
//

import UIKit
import SnapKit
import TCCKit
class EPViewController: UIViewController {
    var backBtn : UIButton?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.bgHexColor = "#F6F7F8"
        self.initNavigationBar()
        self.setupNaviBarColor("#F6F7F8".color(), HEX_BLACK)
        self.addTap()
        self.addNotify()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let hidden = self.className == "EPMineViewController" || self.className == "EPStoreViewController"
        self.navigationController?.setNavigationBarHidden(hidden, animated: animated)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .darkContent
    }
    
    func initUI(){}
    
    func initNavigationBar(){
        self.backBtn = UIButton(type: .custom)
        self.backBtn?.frame = CGRect(x: 0, y: kUIStatusBarHeight + 5, width: 40, height: 34)
        self.backBtn?.addTarget(self, action: #selector(backBtnAction), for: .touchUpInside)
        self.backBtn?.setImage(UIImage(named:"箭头_返回")?.withRenderingMode(.alwaysTemplate), for: .normal)
        self.backBtn?.setImage(UIImage(named:"箭头_返回")?.withRenderingMode(.alwaysTemplate), for: .disabled)
        
        let backItem = UIBarButtonItem(customView: self.backBtn!)
        self.navigationItem.leftBarButtonItem = backItem
        self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.black, .font:font(18)]
    }
    
    func setupNaviBarColor(_ bgcolor:UIColor, _ titleColor:UIColor){
        self.backBtn?.tintColor = titleColor
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = bgcolor
            appearance.backgroundImage = UIImage.withColor(bgcolor)
            appearance.shadowColor = .clear///下划线颜色
            appearance.titleTextAttributes = [.foregroundColor:titleColor,.font:font(16)]
            self.navigationController?.navigationBar.setBackgroundImage(UIImage.withColor(bgcolor), for: .default)
            self.navigationController?.navigationBar.standardAppearance = appearance
            self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
            self.navigationController?.navigationBar.isTranslucent = false
        }else{
            self.navigationController?.navigationBar.shadowImage = UIImage()
            self.navigationController?.navigationBar.barTintColor = bgcolor
            self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor:titleColor]
            self.navigationController?.navigationBar.tintColor = titleColor
            self.navigationController?.navigationBar.isTranslucent = false
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        }
    }
    
    ///添加点击手势
    func addTap(){
        let tap = UITapGestureRecognizer(target: self, action: #selector(actionForTap))
        tap.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tap)
    }
    /// 点击空白处隐藏键盘
    @objc func actionForTap(){
        self.view.endEditing(true)
    }
    
    @objc func backBtnAction(){
        self.navigationController?.popViewController(animated: true)
    }
    
    var tableViewStyle : UITableView.Style = .plain
    @objc lazy var tableView:EPBaseTableView =
    {
        let view = EPBaseTableView(frame:.zero, style:self.tableViewStyle)
        view.separatorStyle = .none
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.backgroundColor = "#F6F7F8".color()
        view.tableHeaderView = UIView(frame: .zero)
        if #available(iOS 11.0, *) {
            view.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        self.view.addSubview(view)
        return view
    }()
    
    var autoAddCollectionView : Bool = true
    var flowLayout = UICollectionViewFlowLayout()
    @objc lazy var collectionView: EPBaseCollectionView =
    {
        let view = EPBaseCollectionView(frame:.zero, collectionViewLayout:self.flowLayout)
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        view.backgroundColor = "#F6F7F8".color()
        if #available(iOS 11.0, *) {
            view.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        if self.autoAddCollectionView{
            self.view.addSubview(view)
        }
        return view
    }()
    
}


//MARK: - 键盘相关通知
extension EPViewController{
    @objc func addNotify(){
        NotifyTool.add(self, #selector(methodKeyboardShow),UIResponder.keyboardWillChangeFrameNotification.rawValue)
        NotifyTool.add(self, #selector(methodKeyboardHidden), UIResponder.keyboardWillHideNotification.rawValue)
    }
    
    @objc func removeNotify(){
        //        NotifyTool.remove(self)
    }
    
    @objc func methodKeyboardShow(_ notify:Notification){
        let userinfo = notify.userInfo
        let value = userinfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue
        let time = (userinfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber) .doubleValue
        let keyboard_H = value.cgRectValue.size.height
        var maxY : CGFloat = 0
        
        let view = self.firstResponder(self.view)
        if view == nil{
            return
        }
        let rect = view!.convert(view!.bounds, to: UIApplication.shared.delegate!.window!)
        maxY = rect.maxY
        
        let map = kUIScreenHeight - maxY - keyboard_H
        if map < 0 {
            UIView.animate(withDuration: time) {
                self.view.transform = CGAffineTransform(translationX: 0, y: map)
            }
        }
    }
    
    @objc func methodKeyboardHidden(_ notify:Notification){
        let time = (notify.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber) .doubleValue
        UIView.animate(withDuration: time) {
            self.view.transform = CGAffineTransform.identity
        }
    }
    
}
